package com.app.controller.backstage;

import cn.hutool.core.date.DateUtil;
import com.app.mapper.CategoryMapper;
import com.app.model.*;
import com.app.service.AdminUserService;
import com.app.service.EntrepreneurshipService;
import com.app.service.MerchantsService;
import com.app.service.RecruitService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 创业“蓝海”
 */
@Controller
@RestController
@RequestMapping("/admin")
public class AdminEntrepreneurshipController {
    @Autowired
    private EntrepreneurshipService entrepreneurshipService;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private RecruitService recruitService;
    @Autowired
    private MerchantsService merchantsService;


    /**
     * 创业招聘
     *
     * @param page    第页
     * @param limit   限制
     * @param keyword 关键字
     * @param request 请求
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/entrepreneurship/recruit")
    public Result<Object> entrepreneurshipRecruit(@RequestParam(value = "page") Integer page,
                                                  @RequestParam(value = "limit") Integer limit,
                                                  @RequestParam(value = "keyword") String keyword,
                                                  HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        QueryWrapper<RecruitModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword);
        queryWrapper.orderByDesc("id");
        return Result.success(recruitService.listByPage(page, limit, queryWrapper));
    }


    /**
     * 插入和更新
     *
     * @param recruitModel 招募模型
     * @param request      请求
     * @return {@link Result}<{@link Object}>
     */
    @PostMapping("/entrepreneurship/insertAndUpdate")
    public Result<Object> insertAndUpdate(@RequestBody RecruitModel recruitModel,
                                          HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (recruitModel.getId() != null && recruitModel.getId() == 0) {
            recruitModel.setCreateTime(DateUtil.date().toTimestamp());
            this.recruitService.insert(recruitModel);
            return Result.success();
        }
        recruitModel.setUpdateTime(DateUtil.date().toTimestamp());
        this.recruitService.update(recruitModel);
        return Result.success();
    }


    /**
     * 删除
     *
     * @param id      id
     * @param request 请求
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/entrepreneurship/del")
    public Result<Object> delete(@RequestParam(value = "id") Integer id,
                                 HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        try {
            this.recruitService.del(id);
        } catch (Exception e) {
        }
        return Result.success();
    }


    /**
     * 招商引资
     *
     * @param page    第页
     * @param limit   限制
     * @param keyword 关键字
     * @param request 请求
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/entrepreneurship/merchants")
    public Result<Object> entrepreneurshipMerchants(@RequestParam(value = "page") Integer page,
                                                    @RequestParam(value = "limit") Integer limit,
                                                    @RequestParam(value = "keyword") String keyword,
                                                    HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        QueryWrapper<MerchantsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword);
        queryWrapper.orderByDesc("id");
        return Result.success(merchantsService.listByPage(page, limit, queryWrapper));
    }


    /**
     * 商家插入和更新
     * 招商引资的插入和更新
     *
     * @param request        请求
     * @param merchantsModel 商家模型
     * @return {@link Result}<{@link Object}>
     */
    @PostMapping("/entrepreneurship/merchants/insertAndUpdate")
    public Result<Object> merchantsInsertAndUpdate(@RequestBody MerchantsModel merchantsModel,
                                                   HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (merchantsModel.getId() != null && merchantsModel.getId() == 0) {
            merchantsModel.setCreateTime(DateUtil.date().toTimestamp());
            this.merchantsService.insert(merchantsModel);
            return Result.success();
        }
        merchantsModel.setUpdateTime(DateUtil.date().toTimestamp());
        this.merchantsService.update(merchantsModel);
        return Result.success();
    }


    @GetMapping("/entrepreneurship/merchants/del")
    public Result<Object> merchantsDelete(@RequestParam(value = "id") Integer id,
                                          HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        this.merchantsService.del(id);
        return Result.success();
    }


    //类型列表
    @GetMapping("/entrepreneurshipTypeList")
    public Result<Object> entrepreneurshipTypeList() {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 1);
        return Result.success(categoryMapper.selectList(queryWrapper));
    }

    //新增和编辑
    @PostMapping("/insertAndUpdateEntrepreneurship")
    public Result<Object> insertAndUpdateVideo(@RequestBody EntrepreneurshipModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (model.getId() > 0) {
            model.setUpdateTime(CommonHelp.getNowTimestamp());
            entrepreneurshipService.update(model);
        } else {
            model.setCreateTime(CommonHelp.getNowTimestamp());
            entrepreneurshipService.insert(model);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteEntrepreneurship")
    public Result<Object> deleteVideo(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        entrepreneurshipService.deleteById(id);
        return Result.success("操作成功");
    }

    //列表
    @GetMapping("/getEntrepreneurshipList")
    public Result<Object> getVideoList(Integer page, Integer limit, String keyword, Integer type, Integer status, Integer township) {
        return Result.success(entrepreneurshipService.selectList(page, limit, keyword, type, status,township));

    }

    //查看详情
    @GetMapping("/getEntrepreneurshipInfo")
    public Result<Object> getVideoInfo(Integer id) {
        return Result.success(entrepreneurshipService.selectById(id));
    }

    //审核
    @GetMapping("/examineEntrepreneurship")
    public Result<Object> examineEntrepreneurship(Integer id, Integer status, String remark, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        EntrepreneurshipModel model = new EntrepreneurshipModel();
        model.setId(id);
        model.setStatus(status);
        model.setRemark(remark);
        entrepreneurshipService.update(model);
        return Result.success("审核成功");
    }
}
